<?php
namespace Home\Controller;
use Think\Controller;
class UserController extends ParentController {
	public function _initialize(){
		parent::_initialize();
	}
    public function index(){
    	$this->assign('user',M('user')->where("username='%s'",session('username'))->find());
        $this->display();
    }

    // 修改资料逻辑
    public function edit(){
    	$user = M('user');
    	if(IS_POST){
    		$username = I('username');
	    	$password = I('password');
	    	$re_password = I('re_password');
	    	if($password!=''){
	    		if($password == $re_password){
	    			$user->password = passwd($password);
	    			$user->username = $username;
	    			if($user->where("username='%s'",session('username'))->save()>0){
	    				$this->success('修改成功');
	    			}else{
	    				$this -> error('修改失败');
	    			}
	    		}else{
	    			$this->error('两次输入的密码不正确');
	    		}
	    	}else{
	    		$user->username = $username;
	    		// var_dump($user->where("username='%s'",session('username'))->save());die();
    			if($user->where("username='%s'",session('username'))->save()>0){
    				$this->success('修改成功');
    			}else{
    				$this -> error('修改失败');
    			}
	    	}
    	}else{
    		$this->assign('user',$user->where("username='%s'",session('username'))->find());
    		$this->display();
    	}
    }
    // 添加用户
    public function add(){
    	if(session('is_admin')==1){
	    	if(IS_POST){
	    		$data['username'] = I('username');
	    		$data['password'] = passwd(I('password'));
	    		$data['re_password'] = passwd(I('re_password'));
	    		$data['identity'] = I('identity');
	    		$data['created_at'] = time();
	    		$data['last_login'] = 0;
	    		$data['is_admin'] = I('is_admin');

	    		// 验证
	    		if($data['password'] == $data['re_password']){
	    			unset($data['re_password']);
	    			if($data['is_admin'] == 'on') $data['is_admin']=1;
	    			else $data['is_admin']=0;
	    		}else{
	    			$this->error('两次输入的密码不相同');
	    		}

	    		// 写入
	    		if(M('user')->data($data)->add()>0){
	    			$this->success('添加成功');
	    		}else{
	    			$this->error('添加失败');
	    		}
	    		
	    	}else{
	    		$this->display();
	    	}
		}else{
			$this->error('您没有权限');
		}
    }
    // 用户列表
    public function lists(){
    	if(session('is_admin')==1){
    	// $users = M('user')->select();
    	$this->assign('users',M('user')->select());
    	$this->display();
    	}else{
			$this->error('您没有权限');
		}
    }
    // 管理员修改用户资料
    public function update($id){
    	if(session('is_admin')==1){
	    	$user = M('user');
	    	if(IS_POST){
	    		$username = I('username');
		    	$password = I('password');
		    	$re_password = I('re_password');
		    	if($password!=''){
		    		if($password == $re_password){
		    			$user->password = passwd($password);
		    			$user->username = $username;
		    			if($user->where("id='%d'",$id)->save()>0){
		    				$this->success('修改成功');
		    			}else{
		    				$this -> error('修改失败');
		    			}
		    		}else{
		    			$this->error('两次输入的密码不正确');
		    		}
		    	}else{
		    		$user->username = $username;
	    			if($user->where("id='%d'",$id)->save()>0){
	    				$this->success('修改成功');
	    			}else{
	    				$this -> error('修改失败');
	    			}
		    	}
	    	}else{
	    		$this->assign('user',$user->where("id='%d'",$id)->find());
	    		$this->display();
	    	}
    	}else{
			$this->error('您没有权限');
		}
    }
    // 管理员删除用户
    public function delete($id){
    	if(session('is_admin')==1){
    	if(IS_DELETE){
    		if((M('user')->where('id="%d"',$id)->delete())>0){
    			return true;
    		}else return false;
    		
    	}else{
    		$this->error('404 NOT FOUND');
    	}
    	}else{
			$this->error('您没有权限');
		}
    }
}